/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xui_lib.component.textview.autoFit;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.app.Context;

/**
 * 能够自适应字体大小的TextView
 *
 * @author xuexiang
 * @since 2019-05-14 22:51
 */
public class AutoFitTextView extends Text implements AutoFitHelper.OnTextSizeChangeListener {
    private AutoFitHelper mHelper;

    /**
     * 构造器
     *
     * @param context
     */
    public AutoFitTextView(Context context) {
        super(context, null);
    }

    /**
     * 构造器
     *
     * @param context
     * @param attrs
     */
    public AutoFitTextView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    /**
     * 构造器
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public AutoFitTextView(Context context, AttrSet attrs, String defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    private void init(AttrSet attrs) {
        mHelper = AutoFitHelper.create(this, attrs);
    }

    /**
     * setTextSize
     *
     * @param size size
     */
    @Override
    public void setTextSize(int size) {
        super.setTextSize(size);
        if (mHelper != null) {
            mHelper.setTextSize(AttrHelper.fp2px(size, getContext()));
        }
    }

    /**
     * setMaxTextLines
     *
     * @param maxLines maxLines
     */
    @Override
    public void setMaxTextLines(int maxLines) {
        super.setMaxTextLines(maxLines);
        if (mHelper != null) {
            mHelper.setMaxLines(maxLines);
        }
    }

    /**
     * Returns the {@link AutoFitHelper} for this View.
     *
     * @return return
     */
    public AutoFitHelper getAutofitHelper() {
        return mHelper;
    }

    /**
     * 是否开启自适应字体大小的功能
     *
     * @return 是否开启自适应字体大小的功能
     */
    public boolean isEnableFit() {
        return mHelper.isEnabled();
    }

    /**
     * 开启自适应字体大小的功能
     */
    public void enableFit() {
        setEnableFit(true);
    }

    /**
     * 设置是否开启自适应字体大小的功能
     *
     * @param enableFit 是否开启
     */
    public void setEnableFit(boolean enableFit) {
        mHelper.setEnabled(enableFit);
    }

    /**
     * 获取最小字体大小【单位： px】
     *
     * @return float
     */
    public float getMinTextSize() {
        return mHelper.getMinTextSize();
    }

    /**
     * 设置自适应的最小字体大小【单位：fp】
     *
     * @param minSize 字体大小
     */
    public void setMinTextSize(int minSize) {
        mHelper.setMinTextSize(minSize);
    }

    /**
     * 字体大小调整精度
     *
     * @return 字体大小调整精度
     */
    public float getPrecision() {
        return mHelper.getPrecision();
    }

    /**
     * 设置自适应的调整精度（精度值越小，越精准，耗时越长）
     *
     * @param precision 字体调整精度
     * @return
     */
    public void setPrecision(float precision) {
        mHelper.setPrecision(precision);
    }

    @Override
    public void onTextSizeChange(float textSize, float oldTextSize) {
        // do nothing
    }
}
